home *** CD-ROM | disk | FTP | other *** search
- Path: ccn.cs.dal.ca!aa254
- From: aa254@ccn.cs.dal.ca (Bill Fraser)
- Newsgroups: comp.lang.c
- Subject: Re: Another What does this do...
- Date: 19 Mar 1996 16:06:44 GMT
- Organization: Chebucto Community Net
- Message-ID: <4imm2k$kv6@apollo.isisnet.com>
- References: <4ijkrh$8t@apollo.isisnet.com> <larry_kearney-1803961050310001@amaryllisp1.appsig.com>
- NNTP-Posting-Host: ccn.cs.dal.ca
- X-Newsreader: TIN [version 1.2 PL2]
-
- To followup...
- I wrote <paraphased somewhat>
- Given:
- : > char *pb;
- : > dop is structure DOP
- What does this do?
- : > pb = (char *)&dop;
-
- Larry Kearney (larry_kearney@appsig.com) wrote:
-
- : What they are doing is simply casting a pointer to the memory allocated to
- : dop for the structure doc_prop (represented by taking the address of dop)
- : into a pointer to a char. This is then assigned to pb which is a variable
- : defined as a pointer to a char.
-
- : If you tried to do something like
-
- : pb = &dop
-
- : the compiler would complain because your are attempting to assign an
- : incorrect type to pb. Casting the pointer as a char eliminates this
- : complaint. I assume that they are attempting to access individual
- : char-sized pieces of the structure through pb (for copying perhaps).
-
- : --
-
- Thanks Larry, and all the others who replied directly too! I
- learned two things about C this week - &, in this case, is NOT
- a bit-wise AND; and casting, not the fishing variety :)
-
- For those who wondered why a structure address was cast to a character
- pointer, pb, it was used later to set either a byte value:
- pb[rgprop[i].offset] = (unsigned char) val;
- or a word value:
- (*(int *) (pb+rgprop[i].offset)) = val;
-
- Not the most obvious ( to me anyway ).
-
- Now, do I learn enough C to modify this code to suit or do I
- revert to Pascal or FORTRAN? Decisions, decisions, decisions :(
-
- Au revoir, Bill
- --
-